package com.tanhua.api.impl.mogo;

import com.tanhua.api.mongo.TaohuaApi;
import com.tanhua.domain.mogo.Sound;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class TaohuaApiImpl implements TaohuaApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdService idService;
    @Override
    public Sound findOneSound(Long userId) {


        System.out.println("桃花传音userId: "+userId);


        Query query = new Query();

        //查找sound所有语音
        List<Sound> list = mongoTemplate.findAll(Sound.class);


        //test 打印list
        for (Sound sound : list) {
            System.out.println("list中的值: "+sound);
        }

        Random random = new Random();
        //随机获取list角标
        int i = random.nextInt(list.size());

        System.out.println("i:"+i);
        //根据list角标获取sound对象
        Sound soundI = list.get(i);
        //获取sound的sid
        //Long sid = soundI.getSid();

        //根据sid 查找
        //Criteria.where("sid").is(sid);
        //Sound sound = mongoTemplate.findOne(query, Sound.class);

        // 取完后，从目标list内移除该数据 避免重复获取
        list.remove(i);

        System.out.println("soundI: "+soundI);
        return soundI;
    }
}
